<?php
/*
* $Header: /PHPMVC/phpmvc-base/WEB-INF/classes/phpmvc/utils/BootUtils.php,v 1.2 2003/10/03 05:00:37 who Exp $
* $Revision: 1.2 $
* $Date: 2003/10/03 05:00:37 $
*
* ====================================================================
*
* License:	GNU Lesser General Public License (LGPL)
*
* Copyright (c) 2002, 2003 John C.Wildenauer.  All rights reserved.
*
* This file is part of the php.MVC Web applications framework
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.

* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.

* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


/**
* Application configuration and setup methods
*
* @author John C. Wildenauer
* @version $Revision: 1.2 $
* @public
*/
class BootUtils {

	/**
	* Load the application configuration
	* <p>Returns an application configuration object (ApplicationConfig)
	*
	* @param ActionServer		The php.MVC controller object reference
	* @param AppServerConfig	The AppServerConfig object reference
	* @param string				The relative path to the phpmvc configuration files
	* @public
	* @returns ApplicationConfig
	*/
	function loadAppConfig(&$actionServer, &$appServerConfig) {

		$initXMLConfig	= False;
		$cfgDataMTime	= 0;
		$cfgXMLMTime	= 0;
		$oApplicationConfig		= '';
		$actionConfigXMLFile	= $actionServer->getConfigPath();
		$actionConfigDataFile	= $actionServer->getConfigPath().'.data';

		if(!file_exists($actionConfigDataFile) ) {	
			// No config data file, so initialise the application
			$initXMLConfig = True;
		} else {
			// Check the config file timestamps
			$cfgDataMTime	= filemtime($actionConfigDataFile);
			$cfgXMLMTime	= filemtime($actionConfigXMLFile);
			if($cfgXMLMTime > $cfgDataMTime) {
				// The 'phpmvc-config.xml' has been modified, so we
				// need to reinitialise the application
				$initXMLConfig = True;
			}
		}

		if($initXMLConfig) {
			// Initialise the php.MVC Web application
			$oApplicationConfig = $actionServer->init($appServerConfig);

			// Serialise the config data
			$strConfig = serialize($oApplicationConfig);
			//$fp = fopen($actionConfigDataFile, 'w');
			//fputs($fp, $strConfig);
			//fclose($fp);

		} else {

			// Unserialise	the application config data
			// 'phpmvc-config.xml' has not been modified, so we
			// do not need to process the xml config data
			$strConfig = implode('', @file($actionConfigDataFile));
			$oApplicationConfig = unserialize($strConfig);


			// Check the class serialise version info. The class structure serialised
			// should ve compatable with the class definition.
			// TO-DO: check all config classes

			// Eg: 'ApplicationConfig:ApplicationConfig.php:20021025-0955
			$strClassID	= $_strClassID = ''; 
			// The instanciated class info
			$strClassID	= $oApplicationConfig->getClassID();
			// The class definition info
			$_strClassID = ApplicationConfig::_getClassID();

			$aClassID = explode(':', $strClassID);	// convert the 'xxx:yyy:nnn' to array()
			$classVersionID = $aClassID[2];			// expecting a class serial date stamp
			$_aClassID = explode(':', $_strClassID);
			$_classVersionID = $_aClassID[2];

			if($_classVersionID != $classVersionID) {
				// Error: php.MVC configuration data stored on disk is not compatable 
				// with this config class
				echo 'Error: php.MVC configuration version stored on disk is not '.
						'compatable with this config class "ApplicationConfig". Please '.
						'regenerate the application configuration data from '.
						'"phpmvc-config.xml"';

				$oApplicationConfig = NULL;
			}

			$actionServer->appServerConfig = $appServerConfig;
		}

		return $oApplicationConfig;

	}


	/**
	* Retrieve the 'action path' part of the request URI
	* <p>Returns the 'action path' eg. "logonForm", or NULL id no 'action path'
	* is found
	*
	* @param _REQ_VARS Array	The HTTP GET or POST vars
	* @param string				The 'action' identifier. Eg 'do', as in 
	*									"index.php?do=logonForm"
	* @public
	* @returns string
	*/
	function getActionPath($_REQ_VARS, $actionID='do') {

		foreach($_REQ_VARS as $varName => $varVal) {

			if($varName != '' && $varName == $actionID) {
				// Allow safe characters only
				if( eregi('^[/a-z0-9_]{1,35}$', $varVal) ) {
					return $varVal;
				}
			}
		}

		return NULL;

	}

}
?>